/*
 * @Author: caihongtai caihongtai
 * @Date: 2025-02-19 10:33:59
 * @LastEditors: caihongtai caihongtai
 * @LastEditTime: 2025-02-27 16:52:02
 * @FilePath: /ai-interview-vue/src/stores/interview.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { defineStore } from 'pinia'
import { InterviewStage } from '@/constants/interview'
import { interviewApi } from '@/api/interview'

export const useInterviewStore = defineStore('interview', {
    state: () => ({
        currentStage: InterviewStage.CHATTING,
        chatHistory: [],
        resumeFile: null,
        selectedJob: null,
        loading: false,
        error: null,
        interviewResult: null,
        interviewHistory: [],
        hasInterviewed: null,
        lastCheckTime: 0,
        checkInterval: 60000
    }),

    actions: {
        resetState() {
            this.currentStage = InterviewStage.CHATTING
            this.chatHistory = []
            this.resumeFile = null
            this.selectedJob = null
            this.interviewResult = null
            this.error = null
            this.hasInterviewed = null
            this.lastCheckTime = 0
        },

        saveInterviewHistory(interview) {
            this.interviewHistory.push(interview)
            // 可以选择保存到本地存储
            localStorage.setItem('interviewHistory', JSON.stringify(this.interviewHistory))
        },

        setStage(stage) {
            this.currentStage = stage
            this.lastActiveTime = new Date().getTime()
        },

        addChatHistory(message) {
            this.chatHistory.push(message)
        },

        setResult(result) {
            this.interviewResult = result
        },

        // 获取面试结果
        async fetchInterviewResult() {
            try {
                this.loading = true
                this.error = null
                // 假设这是面试的相关参数
                const params = {
                    chatHistory: this.chatHistory,
                    duration: this.calculateDuration(),
                    // 其他需要的参数
                }
                const result = await interviewApi.getResult(params)
                this.setResult(result)
                this.setStage(InterviewStage.RESULT)
            } catch (error) {
                console.error('获取面试结果失败:', error)
                this.error = error.message
            } finally {
                this.loading = false
            }
        },

        calculateDuration() {
            // 计算面试时长的逻辑
            return Math.floor((new Date().getTime() - this.lastActiveTime) / 1000)
        },

        // 恢复面试状态
        restoreInterviewState() {
            try {
                const savedState = localStorage.getItem('interviewState')
                if (savedState) {
                    const state = JSON.parse(savedState)
                    this.currentStage = state.currentStage
                    this.chatHistory = state.chatHistory
                    this.interviewResult = state.interviewResult
                    this.lastActiveTime = state.lastActiveTime
                }
            } catch (error) {
                console.error('恢复面试状态失败:', error)
            }
        },

        // 保存面试状态
        saveInterviewState() {
            try {
                const state = {
                    currentStage: this.currentStage,
                    chatHistory: this.chatHistory,
                    interviewResult: this.interviewResult,
                    lastActiveTime: this.lastActiveTime
                }
                localStorage.setItem('interviewState', JSON.stringify(state))
            } catch (error) {
                console.error('保存面试状态失败:', error)
            }
        },

        // 检查是否有面试记录
        async checkInterviewHistory() {
            const now = Date.now()
            if (this.hasInterviewed !== null && (now - this.lastCheckTime) < this.checkInterval) {
                return this.hasInterviewed
            }

            try {
                const res = await interviewApi.getResultList()
                this.hasInterviewed = !!(res.data && res.data.length > 0)

                if (res.data && res.data.length > 0) {
                    this.interviewResult = res.data[0]
                }

                this.lastCheckTime = now
                return this.hasInterviewed
            } catch (error) {
                console.error('获取面试记录失败:', error)
                return false
            }
        }
    }
}) 